set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Network Multimedia MultimediaWidgets WebEngineWidgets Sql)
find_package(Qt6 REQUIRED COMPONENTS LinguistTools)

if (APPLE)
  find_library(FWCoreFoundation CoreFoundation)
  find_library(FWCoreServices CoreServices)
  find_library(FWFoundation Foundation)
  find_library(FWAppKit AppKit)
  find_library(FWIOKit IOKit)
  find_library(FWSecurity Security)
  find_library(FWSystemConfiguration SystemConfiguration)
endif()

set(PROJECT_SOURCES
    aboutdialog.cpp aboutdialog.h
    actionmanager.cpp actionmanager.h
    application.cpp application.h
    contactdialog.h contactdialog.cpp
    disclaimerdialog.h disclaimerdialog.cpp
    globalsettings.cpp globalsettings.h
    globaltype.h
    home/homeview.h home/homeview.cpp
    liveplatforms/douyin.h
    liveplatforms/kuaishou.h
    main.cpp
    main/mainview.h main/mainview.cpp
    main/modules/analyser/analyser.h main/modules/analyser/analyser.cpp
    main/modules/analyser/analyserview.h main/modules/analyser/analyserview.cpp
    main/modules/analyser/analyserview.ui
    main/modules/comparer/comparer.h main/modules/comparer/comparer.cpp
    main/modules/comparer/comparerview.h main/modules/comparer/comparerview.cpp
    main/modules/localdictionary/localdictionaryeditor.h main/modules/localdictionary/localdictionaryeditor.cpp
    main/modules/localdictionary/localdictionaryview.h main/modules/localdictionary/localdictionaryview.cpp
    main/modules/recorder/addattentionsdialog.h main/modules/recorder/addattentionsdialog.cpp
    main/modules/recorder/addattentionsdialog.ui
    main/modules/recorder/addattentionstask.h main/modules/recorder/addattentionstask.cpp
    main/modules/recorder/captchaassistdialog.h main/modules/recorder/captchaassistdialog.cpp
    main/modules/recorder/captchaassisttask.h main/modules/recorder/captchaassisttask.cpp
    main/modules/recorder/getattentionstask.h main/modules/recorder/getattentionstask.cpp
    main/modules/recorder/getliveinfohelper.h main/modules/recorder/getliveinfohelper.cpp
    main/modules/recorder/liveinfoview.h main/modules/recorder/liveinfoview.cpp
    main/modules/recorder/liverecorder.h main/modules/recorder/liverecorder.cpp
    main/modules/recorder/liverecordertype.h
    main/modules/recorder/liverecorderview.h main/modules/recorder/liverecorderview.cpp
    main/modules/recorder/liverecorderview.ui
    main/modules/recorder/livestatusitemdelegate.h main/modules/recorder/livestatusitemdelegate.cpp
    main/modules/recorder/livestatusview.h main/modules/recorder/livestatusview.cpp
    main/modules/recorder/liveviewmanager.h main/modules/recorder/liveviewmanager.cpp
    main/modules/recorder/livewindow.h main/modules/recorder/livewindow.cpp
    main/modules/recorder/moreitemwidget.h main/modules/recorder/moreitemwidget.cpp
    main/modules/recorder/presentermonitor.h main/modules/recorder/presentermonitor.cpp
    main/modules/recorder/recordtask.h main/modules/recorder/recordtask.cpp
    main/modules/recorder/recordtaskmanager.h main/modules/recorder/recordtaskmanager.cpp
    main/modules/recorder/streamrecorder.h main/modules/recorder/streamrecorder.cpp
    main/modules/recorder/streamrecorderconfig.h
    main/modules/recorder/streamrecordercontroller.h main/modules/recorder/streamrecordercontroller.cpp
    main/modules/recorder/streamrecorderwithfailover.h main/modules/recorder/streamrecorderwithfailover.cpp
    main/modules/recorder/useragentgenerator.h
    main/modules/screencapture/screencapture.h main/modules/screencapture/screencapture.cpp
    main/modules/screencapture/screencaptureview.h main/modules/screencapture/screencaptureview.cpp
    main/modules/screencapture/screenlistmodel.cpp main/modules/screencapture/screenlistmodel.h
    main/modules/screencapture/windowlistmodel.cpp main/modules/screencapture/windowlistmodel.h
    services/protocols.h
    services/serverliveinfo.h
    services/serviceurls.h
    services/yingceservice.cpp services/yingceservice.h
    settings/settingsview.h settings/settingsview.cpp
    settings/settingsview.ui
    storage/localstorage.cpp storage/localstorage.h
    storage/livewebentrydao.cpp storage/livewebentrydao.h
    usersettings.cpp usersettings.h
    user/changeuseravatardialog.cpp user/changeuseravatardialog.h
    user/changeuseravatartask.cpp user/changeuseravatartask.h
    user/changeusercoverdialog.cpp user/changeusercoverdialog.h
    user/changeusercovertask.cpp user/changeusercovertask.h
    user/editprofiledialog.cpp user/editprofiledialog.h
    user/editprofiletask.cpp user/editprofiletask.h
    user/editprofileview.cpp user/editprofileview.h user/editprofileview.ui
    utils/initializationtask.h utils/initializationtask.cpp
    user/localuserwidget.cpp user/localuserwidget.h
    user/loginview.cpp user/loginview.h
    user/logindialog.cpp user/logindialog.h user/loginview.ui
    user/registerview.cpp user/registerview.h user/registerview.ui
    utils/keeptokenlive.cpp utils/keeptokenlive.h
    utils/logintask.cpp utils/logintask.h
    utils/removefilesthread.h
    utils/stringutils.cpp utils/stringutils.h
    utils/synchtask.cpp utils/synchtask.h
    utils/utils.cpp utils/utils.h
    version.h
    widgets/checkboxitemdelegate.h widgets/checkboxitemdelegate.cpp
    widgets/nofocusitemdelegate.cpp widgets/nofocusitemdelegate.h
    widgets/pushbutton.h
    widgets/scrollview.cpp widgets/scrollview.h
    widgets/textedit.h
    widgets/titlebutton.h
    widgets/web/bridgeobjectmanager.cpp widgets/web/bridgeobjectmanager.h
    widgets/web/webbridge.cpp widgets/web/webbridge.h
    widgets/web/webcontainer.cpp widgets/web/webcontainer.h
    widgets/web/webinterface.cpp widgets/web/webinterface.h
    widgets/web/webpage.cpp widgets/web/webpage.h
    widgets/web/webview.cpp widgets/web/webview.h
    yingce.cpp yingce.h
    yingcewindow.cpp yingcewindow.h
)

set(PROJECT_SOURCES ${PROJECT_SOURCES} ${TS_FILES})

qt_add_resources(PROJECT_SOURCES yingce.qrc)
qt_add_resources(PROJECT_SOURCES ./resources/resources.qrc)

if (WIN32)
    set(PROJECT_SOURCES ${PROJECT_SOURCES} yingce.rc)
endif()

if(Qt6_VERSION_MAJOR GREATER_EQUAL 6)
    qt_add_executable(YingCe
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
        main/modules/recorder/segmentsmerger.h main/modules/recorder/segmentsmerger.cpp
        user/changepassworddialog.h user/changepassworddialog.cpp user/changepassworddialog.ui
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET YingCe APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    message(WARNING ${QT_VERSION_MAJOR})
    if(ANDROID)
        add_library(YingCe SHARED ${PROJECT_SOURCES})
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(YingCe ${PROJECT_SOURCES})
    endif()
endif()

if (Qt6_VERSION VERSION_LESS 6.7.0)
    qt_add_translations(YingCe
        TS_FILES YingCe_en_US.ts YingCe_zh_CN.ts
        QM_FILES_OUTPUT_VARIABLE qm_files
        INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src/app ${PROJECT_SOURCE_DIR}/src/libs
        LUPDATE_OPTIONS -no-obsolete)
else()
    qt_add_translations(YingCe
        SOURCE_TARGETS YingCe Base Controls Core
        TS_FILE_BASE YingCe
        NO_GENERATE_PLURALS_TS_FILE
        LUPDATE_OPTIONS -no-obsolete)
endif()

target_include_directories(YingCe PRIVATE
    ${Qt6Quick_PRIVATE_INCLUDE_DIRS}
    ${Qt6QuickTimeline_PRIVATE_INCLUDE_DIRS}
    ${Qt6Quick3D_PRIVATE_INCLUDE_DIRS}
    ${Qt6Quick3DAssetImport_PRIVATE_INCLUDE_DIRS}
    ${Qt6Quick3DParticles_PRIVATE_INCLUDE_DIRS}
    ${Qt6Quick3DAssetUtils_PRIVATE_INCLUDE_DIRS}
    ${PROJECT_SOURCE_DIR}/src
    ${PROJECT_SOURCE_DIR}/src/app
    ${PROJECT_SOURCE_DIR}/src/libs
    ${PROJECT_SOURCE_DIR}/src/libs/thirdparty
)

# Resources(icon, translators, fonts ...)
set(RESOURCE_FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/resources/translations/qtbase_zh_CN.qm"
    "${CMAKE_CURRENT_SOURCE_DIR}/resources/disclaimer.html"
)

if (APPLE)
    set(RESOURCE_FILES ${RESOURCE_FILES}
        "${CMAKE_CURRENT_SOURCE_DIR}/yingce.icns"
    )
endif()

#if (WIN32)
#    string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
#    if (build_type STREQUAL release)
#        set(RESOURCE_FILES ${RESOURCE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/qt.conf")
#    endif()
#endif()

target_sources(YingCe PRIVATE ${RESOURCE_FILES})

if (WIN32)
    foreach(i ${RESOURCE_FILES})
        FILE(COPY "${i}" DESTINATION "${PROJECT_BINARY_DIR}/Resources")
    endforeach(i)
endif()

set_target_properties(YingCe PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER com.yingce
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE_COPYRIGHT "yingce.io"
    MACOSX_BUNDLE_ICON_FILE "yingce.icns"
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
    RESOURCE "${RESOURCE_FILES}"
)

target_compile_definitions(YingCe PRIVATE

)

target_link_libraries(YingCe PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
    Qt6::Network
    Qt6::Multimedia
    Qt6::MultimediaWidgets
    Qt6::WebEngineWidgets
    Qt6::Sql
)

if (WIN32)
    string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
    if (build_type STREQUAL release)
        target_link_directories(YingCe PRIVATE "${PROJECT_SOURCE_DIR}/src/libs/thirdparty/imageutils/MSVC2022 64bit/Release")
        target_link_directories(YingCe PRIVATE "${PROJECT_SOURCE_DIR}/src/libs/thirdparty/crashrpt/lib/x64")
        target_link_libraries(YingCe PRIVATE CrashRpt1403 CrashrptHelper)
    else()
        target_link_directories(YingCe PRIVATE "${PROJECT_SOURCE_DIR}/src/libs/thirdparty/imageutils/MSVC2022 64bit/Debug")
        # target_link_libraries(YingCe PRIVATE CrashRpt1403d CrashrptHelper)
    endif()
endif()

# get_target_property(result YingCe LINK_DIRECTORIES)
# message("result: ${result}")

target_link_libraries(YingCe PRIVATE tinyxml imageutils Base Controls Core Extension)

if(Qt6_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(YingCe)
endif()

install(TARGETS YingCe
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RESOURCE DESTINATION "./Resources"
)
